package agents;

import env.Enviroment;
import env.Action;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.ImageIcon;

/**
 *
 * @author orcas
 */
public abstract class Agent {

    private String name;
    public int score = 0;
    private AgentState state;
    private boolean isAlive = true;
    private static Icon defaultIcon =new ImageIcon("images/AgentNorthRed.GIF");
    private Icon icon = defaultIcon;

    public Agent(String name, int row, int col) {
        this.name = name;
        this.state = new AgentState(row, col, null);
    }

    public abstract Action getMove();

    public String getName() {
        return name;
    }

    public int getScore() {
        return score;
    }

    public AgentState getState() {
        return state;
    }

    public boolean isAlive() {
        return isAlive;
    }

    public Icon getIcon() {
        return icon;
    }

    /**
     * overide to change!
     * @return
     */
    public boolean isArmed() {
        return false;
    }

    public void kill() {
        this.isAlive = false;
        state.getTile().setAgent(this);
        System.out.println(this + " is dead!  on step " + Enviroment.step);
        try {
            System.in.read();
        } catch (IOException ex) {
            Logger.getLogger(Agent.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void updatePosition(Action move) {
        state.getTile().setAgent(null);
        state.updatePosition(move);
        state.getTile().setAgent(this);
        score--;
    }

    public void setIcon(Icon icon) {
        this.icon=icon;
    }

    @Override
    public String toString() {
        return (isAlive?"":"DEAD ")+"Agent " + getName() + ": " + state + ",   score=" + score;
    }
}
